package com.hit.RuningGirl.handle;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.Contact;
import com.badlogic.gdx.physics.box2d.ContactImpulse;
import com.badlogic.gdx.physics.box2d.ContactListener;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.Manifold;
import com.badlogic.gdx.utils.Array;

public class Box2DContactListener implements ContactListener {


	private int platformNum;

	private boolean flameContact;

	private Array<Body> removeBodies;

	private boolean trapContact;

	
	public Box2DContactListener() {
		super();
		removeBodies = new Array<Body>();

	}

	@Override
	public void beginContact(Contact contact) {

		Fixture fixtureA = contact.getFixtureA();

		Fixture fixtureB = contact.getFixtureB();

		if (fixtureA.getUserData() != null && fixtureA.getUserData().equals("foot")) {

			platformNum++;
		}
		if (fixtureB.getUserData() != null && fixtureB.getUserData().equals("foot")) {

			platformNum++;
		}
		
		if (fixtureA.getUserData() != null && fixtureA.getUserData().equals("heart")) {

			removeBodies.add(fixtureA.getBody());
		}

		if (fixtureB.getUserData() != null && fixtureB.getUserData().equals("heart")) {

			removeBodies.add(fixtureB.getBody());
		}
		if (fixtureA.getUserData() != null && fixtureA.getUserData().equals("flame")) {

			flameContact = true;
		}
		
		if (fixtureB.getUserData() != null && fixtureB.getUserData().equals("flame")) {

			flameContact = true;
		}

		if (fixtureA.getUserData() != null && fixtureA.getUserData().equals("trap")) {

			trapContact = true;
		}

		if (fixtureB.getUserData() != null && fixtureB.getUserData().equals("trap")) {

			trapContact = true;
		}
	}

	public void endContact(Contact contact) {

		Fixture fixtureA = contact.getFixtureA();

		Fixture fixtureB = contact.getFixtureB();

		if (fixtureA == null || fixtureB == null)
			return;

		if (fixtureA.getUserData() != null && fixtureA.getUserData().equals("foot")) {

			platformNum--;
		}
		if (fixtureB.getUserData() != null && fixtureB.getUserData().equals("foot")) {

			platformNum--;
		}
	}

	public boolean isOnPlatform() {

		return platformNum > 0;
	}
	

	public boolean isContactFlame(){
		return flameContact;
	}

	public boolean isTrapContact(){
		return trapContact;
	}

	public Array<Body> getRemoveBodies() {
		return removeBodies;
	}

	@Override
	public void preSolve(Contact contact, Manifold oldManifold) {
	}

	@Override
	public void postSolve(Contact contact, ContactImpulse impulse) {
	}

}
